Markov chain


In [19]:
import numpy as np

class Markov:
    def __init__ (self, order):
        self.order = order
        self.group_size = self.order + 1
        self.text = None
        self.graph = {}
        return

    def fit(self, filename): 
        self.text = open(filename).read().split() 
        self.text = self.text + self.text [:self.order]

        for i in range (0, len (self.text) - self.group_size):
            key = tuple (self.text [i : i + self.order] ) 
            value = self.text [i + self.order]

            if key in self.graph:
                self.graph[key].append (value)
            else:
                self.graph[key] = [value]    
        return

    def generate (self,length):
        index = np.random.randint(0, len(self.text) - self.order)
        result = self.text[index : index + self.order]

        for i in range(length):
            state = tuple(result[len(result) - self.order:])
            next_word = np.random.choice(self.graph[state])
            result.append(next_word)
        
        return " ".join(result[self.order:])


markov = Markov(3)
markov.fit("data/Bible.txt")
markov.generate(100)


Out[19]:
"LORD overthrew, and repented not: and let him say, Is it peace? And Jehu said, Unto which of all us? And he said unto him, This is the ordinance of their God: but these have altogether broken the yoke, and burst the bonds. 5:6 Wherefore a lion out of the wells of water, which Abimelech's servants had violently taken away. 21:26 And Abimelech said, Behold, my master wotteth not what is become of him. 32:2 And when Jacob had made an end of the sabbath, as it began to dawn toward the first day shall be a sign unto thee,"

In [ ]: